<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@page contentType="text/html; charset=utf-8"%>
<%@page import="com.yc.comm.Constant"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    <link rel="stylesheet" type="text/css" href="<%=path%>/js/ext/resources/css/ext-all.css">  
	<link rel="stylesheet" type="text/css" href="<%=path%>/js/ext/resources/css/edit-grid.css">  
	<script type="text/javascript" src="<%=path%>/js/ext/adapter/ext/ext-base.js"></script>
	<script type="text/javascript" src="<%=path%>/js/ext/ext-all.js"></script>
	<script type="text/javascript" src="<%=path%>/js/ext/DWRTreeLoader.js"></script>
	<script type="text/javascript" src="<%=path%>/js/ext/query-dwr-proxy.js"></script>	
	<script type='text/javascript' src='/btsm/dwr/interface/JCommWeb.js'></script>
	<script type='text/javascript' src='/btsm/dwr/engine.js'></script>
	<script type='text/javascript' src='/btsm/dwr/util.js'></script>	 	

  </head>
  
 <body>
   <div id="queryCondition"/>  
   <div id="dictContent" style="width:100%;height:100%"/>   
<script type="text/javascript">

var ds;
Ext.onReady(function(){
	Ext.QuickTips.init(); 
	Ext.form.Field.prototype.msgTarget = 'side'; 
	var viewport = new Ext.Viewport({
		    layout:'border',
		    items:[	    	
		    	{	region: 'north',
			        contentEl: 'queryCondition',
			        margins:'120 0 0 5',
			        split: true,
		        	border: false,
		        	collapsible: false,
			        width:20
			    },	    	
		    	{	region: 'center',
			        contentEl: 'dictContent',
			        margins:'25 0 30 5',
			        split: true,
			        border: false,
			        collapsible: true
			    }]
			});
		
	var sm = new Ext.grid.CheckboxSelectionModel({singleSelect: true});	
	var recordType = Ext.data.Record.create([								
							   {name: 'id', type: "string"},
							   {name: 'dictype', type: "string"},
							   {name: 'dictypename', type: "string"},
							   {name: 'diccode', type: "string"},
							   {name: 'dicname', type: "string"}
						 ]);

	var cm  = new Ext.grid.ColumnModel(
			[
				sm,            
				{header: "流水号", width: 80, sortable: true, dataIndex: 'id',align:'left'},
				{header: "类型编号", width: 140, sortable: true, dataIndex: 'dictype',align:'left'},
				{header: "类型名称", width: 140, sortable: true, dataIndex: 'dictypename',align:'left'},	
				{header: "字典编号", width: 140, sortable: true, dataIndex: 'diccode',align:'left'}, 
				{header: "字典名称", width: 140, sortable: true, dataIndex: 'dicname',align:'left'}, 				
				{header: "删除", width: 50, sortable: false, dataIndex: 'id',renderer: showDelete}				
			]
		);
	function showDelete(id)
	{
		return "<input type='button' value='删除' class='x-btn button' onclick='doDeleteDict(\"" + id  +   "\")'>";  
	}		
	
	ds = new Ext.data.Store({
			proxy: new Ext.data.DWRProxy(JCommWeb.getDicList, true),	
			reader: new Ext.data.ListRangeReader({
				totalProperty: 'totalSize',
				id: 'id'
			}, recordType),
			sortInfo:{field:'id', direction:'ASC'}// 排序信息
		});	
		
	var btnAddDict = new  Ext.Button({
					text:' 创  建 ',
					pressed:true,					 
					handler:showCreateDict
				});
	var btnEditDict = new  Ext.Button({
					text:' 修  改 ',
					pressed:true,					 
					handler:showEditDict
				});		
	function showCreateDict(){	
		var data = {};			
		var record = new Ext.data.Record(data);
		dictWindow.show();
		dictWindow.formPanel.getForm().loadRecord(record);		
	}

	function showEditDict(){
		var record = grid.getSelectionModel().getSelected();
		if(!record)
		{
			Ext.Msg.alert("提示","请先选择需要修改的字典信息 !");
			return;
		}
		dictWindow.show();
		dictWindow.formPanel.getForm().loadRecord(record);
	}
	
	
	var dictTypeStore  = new Ext.data.Store({  
		        proxy:new Ext.data.DWRProxy(JCommWeb.qetDictTypeList,{}),  
		        reader:new Ext.data.DWRArrayReader(
						{},
						new Ext.data.Record.create([  
							{name: 'text', mapping:1},       
							{name: 'value',mapping:0}                   
						  ])
						)  
		    }); 
	
	var dictTypeCombo = new Ext.form.ComboBox({	   
		fieldLabel: '字典类型',
		name: 'landform',	
		width:200,
		valueField :"value",
	    displayField: "text",
	    mode: 'remote',
		selectOnFocus:true,                    
		forceSelection: true, 
		hiddenName:'landform',
		triggerAction: 'all', 
		editable:false, 
		store: dictTypeStore
	}); 
	dictTypeStore.load();
	var QueryCondition = new Ext.FormPanel({
	    	labelAlign: 'right',
	        frame:true, 
	        labelWidth:80,	       
			anchor:'90%',
			height:450,		
			buttonAlign:'right',
			collapsible:true,
			border: false,
			layout:'form',
			margins:'35 5 5 0',	
	        items: [dictTypeCombo]
		})	
	QueryCondition.render('queryCondition');
	dictTypeCombo.on('select',function(){ 
		var jobType = dictTypeCombo.getValue();
		 
		ds.on('beforeload', function() 
		{
			ds.baseParams = {'query':jobType};
		});
		ds.load({params:{'query':jobType,start:0,limit:15}});		 
	});
	
	
	var contentDiv = Ext.get("dictContent");
	var grid = new Ext.grid.GridPanel({
	    	el: 'dictContent',	    	
	        ds: ds,
	        cm: cm,
			sm:sm,
			//title: '字典列表',
		    width: contentDiv.getComputedWidth(),
			height: contentDiv.getComputedHeight(), 
	        frame:true,
	        collapsible:false, 
	        enableDragDrop:false, 
			tbar: [
					btnAddDict,
					'-',
					btnEditDict
					 
				],
			bbar: new Ext.PagingToolbar({
			        pageSize: 15,
			        store: ds,
			        displayInfo: true,
			        displayMsg: '显示第 {0} 条到 {1} 条记录，一共 {2} 条',
			        emptyMsg: "没有记录"
			    })
					
				
	    });
	    
	
    grid.render(); 
	ds.load({params:{start:0,limit:15}});	
	
	var DictForm = new Ext.FormPanel({
	    	labelAlign: 'right',
	        frame:true,	        
	        labelWidth:80,	       
			anchor:'90%',
			height:350,		
			buttonAlign:'right', 
			border: false,
			layout:'form',
			margins:'35 35 35 35',	
	        items: [ 
					{
						xtype : 'hidden',
						fieldLabel : "id",
						readOnly : true,
						name : "id"
					},
					{
						xtype:'textfield',
						fieldLabel: '类型编号 ',
						name: 'dictype',
						width:200, 	  
						maxLength:2, 
						allowBlank:false,
						blankText :'请输入类型编号'
					},{
						xtype:'textfield',
						fieldLabel: '类型名称 ',
						name: 'dictypename',
						width:200,   
						allowBlank:false,
						blankText :'请输入类型名称'
					},					
					{
						xtype:'textfield',
						fieldLabel: '字典编号',
						name: 'diccode',
						maxLength:6, 
						width:200, 	 
						allowBlank:false,
						blankText :'请输入字典编号'
					},					
					{
						xtype:'textfield',
						fieldLabel: '字典名称',
						name: 'dicname',
						width:200, 	 
						allowBlank:false,
						blankText :'请输入字典名称'
					}
			]
		})	
		
		
	var DictWindow = function() {
		var _win = this;
		this.formPanel = DictForm;
		var _form = this.formPanel.getForm();
		DictWindow.superclass.constructor.call(this, { 
			title: '字典信息',
			width : 350,
			height : 230,
			resizable : true,
			plain : false,
			border : false,
			modal : true,
			autoScroll : false,
			layout : 'fit',
			closeAction : 'hide',
			items : this.formPanel,
			buttonAlign:'center',
			buttons : 
			[
				{	text : ' 保存',
					handler : function() 
					{
						if (_form.isValid()) 
						{
							var form = _form.getValues(false);							 
							DictionaryBO = {};
							DictionaryBO.id = form.id;	
							DictionaryBO.dictype = form.dictype;	
							DictionaryBO.dictypename = form.dictypename;	
							DictionaryBO.diccode = form.diccode;	
							DictionaryBO.dicname = form.dicname;	
							 
							JCommWeb.saveDict(DictionaryBO,function(data)
							{
								if(data)
	          					{
	          						_win.hide();
	          						Ext.Msg.alert('成功',"操作成功！",function()
	          						{
										ds.load({params:{start:0,limit:15}});	
	          						}); 
									_form.reset(); 
	          					}else
	          					{
	          						Ext.Msg.alert("失败","操作失败!");
	          					}																
							})							
							
						}
					}
				},{
					text : '取消',
					handler : function() {
						_win.hide();
						_form.reset(); 
					},
					tooltip : '关闭对话框'
				}
			]	
		});
	}
	Ext.extend(DictWindow, Ext.Window); 
	var dictWindow = new DictWindow();	
	
		
})		

function doDeleteDict(id){
	Ext.MessageBox.confirm('确认', '确认要删除?', 
		function(btn) {
		 if(btn == 'yes') // 选中了是按钮	
		 {   	
			JCommWeb.deleteDic(id, function(data) {
				 if(data) {							    	     		
					 ds.load({params:{start:0,limit:15}});
				 }else 
				 {
					Ext.Msg.alert("操作失败"," 删除字典操作失败!");
				 }
			 });
			
		 }
	});
}

</script>   
  
   
 </body>
 
</html>
